package com.example.myapplication1.threademo.threadtest;

import android.util.Log;


import androidx.constraintlayout.motion.widget.MotionLayout;

import com.example.myapplication1.threademo.productinterface.Custom;
import com.example.myapplication1.threademo.productinterface.Product;


/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/12/1 16:55
 * @last-modify-date: 2020/12/1 16:55
 * @description:
 */
public class ThreadTest {


    private static final String TAG = "ThreadTest";
    private int MAX_COUNT = 10;
    //    目前的数量
    private int currentCount;
    //    锁
    private Object object = new Object();


    public void test() throws InterruptedException {

        Log.e("tag", "ceshi");



    }


    public class Customer extends Thread implements Custom {

        @Override
        public void custom() {
//  消费者 如果目前的数量小于0 ,就等待,否者就消费一个

            while (true) {
                synchronized (object) {

                    if (currentCount <= 0) {

                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();

                        }
                        Log.e("tag", "目前没有可消费的苹果,等待");

                    } else {

                        currentCount--;
                        Log.e("tag", "消费者消费了1个苹果,剩余" + currentCount + "个苹果");
                        object.notifyAll();

                    }
                }
            }
        }


    }

    public class Producter extends Thread implements Product {

        @Override
        public void product() {


//             生产者 如果目前的苹果大于最大数量，等待消费者消费后在生产，如果生产的苹果加上剩下的苹果大于了最大数量，应该就生产最大数量

        }

        @Override
        public void run() {
            super.run();
            while (true) {
                synchronized (object) {
                    if (currentCount >= MAX_COUNT) {

                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e("tag", "生产的苹果已经达到最大数量了，等消费者消费");
                    } else {
                        currentCount += Math.random() * 10;
                        Log.e("tag", "生产了" + currentCount + "个苹果");
                        object.notifyAll();
                    }
                }
            }

        }
    }



    public class Callable implements java.util.concurrent.Callable{

        @Override
        public Object call() throws Exception {

            while (true) {
                synchronized (object) {
                    if (currentCount >= MAX_COUNT) {


                            object.wait();

                        Log.e("tag", "生产的苹果已经达到最大数量了，等消费者消费");
                    } else {
                        currentCount += Math.random() * 10;
                        Log.e("tag", "生产了" + currentCount + "个苹果");
                        object.notifyAll();
                    }
                }
                return true;
            }
        }
    }
}
